package com.example.java.newFeature.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class VirtualThreadDemo {
    public static void main(String[] args) {
        // 创建一个虚拟线程执行任务
        Thread.ofVirtual().start(() -> {
            System.out.println("Hello from virtual thread!");
        });

        // 或者使用静态方法
        Thread.startVirtualThread(() -> {
            System.out.println("Hello from virtual thread!");
        });

        // 设置线程名称
        Thread.ofVirtual().name("virtual-thread", 1).start(() -> {
            System.out.println("Hello from virtual thread!");
        });

        // 使用线程工厂创建线程
        Thread.ofVirtual().factory().newThread(() -> {
            System.out.println("Hello from virtual thread!");
        });

        // 使用 ExecutorService 创建线程，每一个任务都会分配一个虚拟线程
        ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
        executorService.execute(() -> {
            System.out.println("Hello from virtual thread!");
        });
    }
}
